package com.sjf.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjf.common.Result;
import com.sjf.reggie.entity.Category;
import com.sjf.reggie.entity.Dish;
import com.sjf.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author <huangtang>
 * @since 2022/4/26 10:29
 **/
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;




    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping()
    public Result<String> save( @RequestBody Category category) {
        log.info ("save category:{}", category);
        categoryService.save (category);
        return Result.success ("新增分类成功");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping()
    public Result<String> update( @RequestBody Category category) {
        log.info ("修改分类:{}", category);
        categoryService.updateById (category);
        return Result.success ("修改分类成功");
    }

    /**
     * 删除分类
     * @param id id
     * @return
     */
    @DeleteMapping()
    public Result<String> delete(Long id) {
        log.info ("删除分类:{}", id);
        boolean result =  categoryService.deleteById (id);
        return result? Result.success ("删除分类成功"):Result.error ("删除分类失败");
    }

    /**
     * 获取菜品分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> getList( Category category){
        log.info ("获取菜品分类");
        List<Category> dishList = categoryService.getList(category);
        return Result.success (dishList);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Category>> page( @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize") Integer pageSize) {
        log.info ("pageNum:{},pageSize:{}", pageNum, pageSize);
        Page<Category> page = categoryService.selectList (pageNum, pageSize);
        return Result.success (page);
    }
}
